package cn.dbwork.yacht.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.dbwork.yacht.dao.IUserDao;
import cn.dbwork.yacht.domain.User;
import cn.dbwork.yacht.utils.ConnectionFactory;

public class UserDao implements IUserDao {
	
	private static Connection con=null;
	private static PreparedStatement stm = null;
	private static ResultSet result = null;
	
	@Override
	public User findStuAndPass(String account, String password) {
		//获取连接
		con=ConnectionFactory.getConnection();
		//查询sql
		String sql="select * from user where account = ? and password = ? and disabled = 0";
		try {
			con=ConnectionFactory.getConnection();
			stm = con.prepareStatement("select * from user where account = ? and password = ? and disabled = 0");
			stm.setString(1, account);
			stm.setString(2, password);
			
			result=stm.executeQuery();
			
			while(result.next()) {
				User user=new User();
				user.setId(result.getInt("id"));
				user.setAccount(result.getString("account"));
				user.setPassword(result.getString("password"));
				user.setName(result.getString("name"));
				user.setSex(result.getString("sex"));
				user.setAge(result.getInt("age"));
				user.setTel(result.getString("tel"));
				user.setRoleId(result.getInt("role_id"));
				user.setLevel(result.getInt("level"));
				user.setCreateUserId(result.getInt("create_user_id"));
				user.setGroupId(result.getInt("group_id"));
				user.setDisabled(result.getInt("disabled"));
				return user;
			//  QWQ
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
		
		return null;
	}

	@Override
	public User findById(int id) {
		// TODO 自动生成的方法存根
		try {
			con=ConnectionFactory.getConnection();
			stm =con.prepareStatement("select * from user where id = ?");
			stm.setInt(1, id);
			result=stm.executeQuery();
			while(result.next()) {
				User user=new User();
				user.setId(result.getInt("id"));
				user.setAccount(result.getString("account"));
				user.setPassword(result.getString("password"));
				user.setName(result.getString("name"));
				user.setSex(result.getString("sex"));
				user.setAge(result.getInt("age"));
				user.setTel(result.getString("tel"));
				user.setRoleId(result.getInt("role_id"));
				user.setLevel(result.getInt("level"));
				user.setCreateUserId(result.getInt("creat_user_id"));
				user.setGroupId(result.getInt("group_id"));
				user.setDisabled(result.getInt("disabled"));
				//  QWQ
				return user;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
		return null;
	}

	@Override
	public void saveMem(User user) {
		// TODO 自动生成的方法存根
		try {
			con=ConnectionFactory.getConnection();
			stm =con.prepareStatement("insert into user(account,password,name,sex,age,tel,role_id,level,create_user_id,group_id,disabled)"
					+ "values(?,?,?,?,?,?,?,?,?,?,?)");
			stm.setString(1, user.getAccount());
			stm.setString(2, user.getPassword());
			stm.setString(3,user.getName());
			stm.setString(4, user.getSex());
			stm.setInt(5, user.getAge());
			stm.setString(6, user.getTel());
			stm.setInt(7, user.getRoleId());
			stm.setInt(8, user.getLevel());
			stm.setInt(9, user.getCreateUserId());
			stm.setInt(10, user.getGroupId());
			stm.setInt(11, user.getDisabled());
			
			stm.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
	}

	@Override
	public void memNew(User user) {
		// TODO Auto-generated method stub
		try {
			con=ConnectionFactory.getConnection();
			stm =con.prepareStatement("insert into user(account,password,name,sex,age,tel,role_id,level,create_user_id,group_id,disabled)"
					+ "values(?,?,?,?,?,?,3,1,0,0,0)");
			stm.setString(1, user.getAccount());
			stm.setString(2, user.getPassword());
			stm.setString(3,user.getName());
			stm.setString(4, user.getSex());
			stm.setInt(5, user.getAge());
			stm.setString(6, user.getTel());
			
			stm.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
	}
	

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		try {
			con=ConnectionFactory.getConnection();
			stm =con.prepareStatement(
					"update user set account = ?,password = ?,name = ?," + 
					" sex = ?,age = ?,tel = ?,role_id = ?,level = ?," + 
					" create_user_id = ?,group_id = ?,disabled = ?" + 
					" where id = ?");
			stm.setString(1, user.getAccount());
			stm.setString(2, user.getPassword());
			stm.setString(3,user.getName());
			stm.setString(4, user.getSex());
			stm.setInt(5, user.getAge());
			stm.setString(6, user.getTel());
			stm.setInt(7, user.getRoleId());
			stm.setInt(8, user.getLevel());
			stm.setInt(9, user.getCreateUserId());
			stm.setInt(10, user.getGroupId());
			stm.setInt(11, user.getDisabled());
			stm.setInt(12, user.getId());
			

			stm.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
	}
	

/**
 * 未完成
 */
	@Override
	public User findByTel(String tel, User userCur) {   
		// TODO 自动生成的方法存根
		try {
			con=ConnectionFactory.getConnection();
			stm =con.prepareStatement("select * from user where tel = ?");
			stm.setString(1, tel);
			result=stm.executeQuery();
			while(result.next()) {
				User user=new User();
				user.setId(result.getInt("id"));
				user.setAccount(result.getString("account"));
				user.setPassword(result.getString("password"));
				user.setName(result.getString("name"));
				user.setSex(result.getString("sex"));
				user.setAge(result.getInt("age"));
				user.setTel(result.getString("tel"));
				user.setRoleId(result.getInt("role_id"));
				user.setLevel(result.getInt("level"));
				user.setCreateUserId(result.getInt("creat_user_id"));
				user.setGroupId(result.getInt("group_id"));
				user.setDisabled(result.getInt("disabled"));
				//  QWQ
				return user;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
		return null;
	}


	@Override
	public Integer checkFromTel(String tel) {
		// TODO Auto-generated method stub
		try {
			con=ConnectionFactory.getConnection();
			stm =con.prepareStatement("select disabled from user where tel = ?");
			stm.setString(1, tel);
			result=stm.executeQuery();
			while(result.next()) {
				return result.getInt("disabled");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionFactory.close(con, stm, result);
		}
		return null;
	}
		
	

}
